/**	
 * <br>
 * Copyright 2011 Chuan. All rights reserved.<br>
 * <br>			 
 * Package: com.chuan.mantu.extend <br>
 * FileName: InfoTipAsyncTask.java <br>
 * <br>
 * @version
 * @author javacyc@qq.com
 * @created 2013-3-11
 * @last Modified 
 * @history
 */

package com.chuan.mantu.extend;


import com.chuan.mantu.R;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.view.Gravity;
import android.view.WindowManager;
import android.widget.TextView;

/**
 * {异步处理页面显示的提示信息}
 *  
 *  @author javacyc@qq.com
 *  @created 2013-3-11 上午10:26:10
 *  @lastModified       
 *  @history           
 */

public class InfoTipAsyncTask extends AsyncTask<String, String, String> {

	private TextView tv;
	private WindowManager wm;
	private WindowManager.LayoutParams params;
	private Context context;
	
	public InfoTipAsyncTask(Context ct, int resId) {
		context = ct;
		wm = (WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
		tv = new TextView(context);
		tv.setText(context.getString(resId));
		tv.setGravity(Gravity.CENTER);
		BitmapDrawable bitmapDrawable = new BitmapDrawable(BitmapFactory.decodeResource(context.getResources(), R.drawable.bottombg));
		bitmapDrawable.setAlpha(174);
		tv.setBackgroundDrawable(bitmapDrawable);
		tv.setTextSize(13.0f);
		tv.setTextColor(Color.WHITE);
		params = new WindowManager.LayoutParams();
		params.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
		params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
		params.format = PixelFormat.RGBA_8888;
		params.gravity = Gravity.LEFT | Gravity.BOTTOM;
		params.width = (int)tv.getPaint().measureText(tv.getText().toString()) + 20;
		params.height = 40;
		params.x = (wm.getDefaultDisplay().getWidth() / 2) - (params.width / 2);
		params.y = wm.getDefaultDisplay().getHeight() / 6;
		params.windowAnimations = android.R.style.Animation_Toast;
	}

	@Override
	protected void onPreExecute() {
		wm.addView(tv, params);
		super.onPreExecute();
	}
	
	@Override
	protected String doInBackground(String... p) {
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	protected void onPostExecute(String result) {
		wm.removeView(tv);
		super.onPostExecute(result);
	}

}